package com.billy.ohos.loadingstatusview.wrapability.adapter.view;

import com.billy.ohos.loading.Gloading;
import com.billy.ohos.loadingstatusview.ResourceTable;
import com.billy.ohos.loadingstatusview.util.Util;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

/**
 * simple loading status view for global usage
 *
 * @author billy.qi
 * @since 19/3/19 23:12
 */
public class GlobalLoadingStatusView extends DirectionalLayout implements Component.ClickedListener {

    private final Text mTextView;
    private final Runnable mRetryTask;
    private Image mImageView;
    private AnimatorValue animator;

    public GlobalLoadingStatusView(Context context, Runnable retryTask) {
        super(context);
        setOrientation(VERTICAL);
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_view_global_loading_status, this, true);
        setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));
        mImageView = (Image) findComponentById(ResourceTable.Id_image);
        mTextView = (Text) findComponentById(ResourceTable.Id_text);
        this.mRetryTask = retryTask;

        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(240, 240, 240));
        setBackground(element);

        animator = new AnimatorValue();
        animator.setDuration(1000);
        animator.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
        animator.setLoopedCount(Integer.MAX_VALUE);
        animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                // 旋转imageView
                mImageView.setRotation(360 * value);
            }
        });
    }

    public void setMsgViewVisibility(boolean visible) {
        mTextView.setVisibility(visible ? VISIBLE : HIDE);
    }

    public void setStatus(int status) {
        boolean show = true;
        ClickedListener onClickListener = null;
        int image = ResourceTable.Media_icon_loading;
        int str = ResourceTable.String_str_none;
        switch (status) {
            case Gloading.STATUS_LOAD_SUCCESS:
                stopImgRotation();
                show = false;
                break;
            case Gloading.STATUS_LOADING:
                str = ResourceTable.String_loading;
                startImgRotation();
                break;
            case Gloading.STATUS_LOAD_FAILED:
                stopImgRotation();
                str = ResourceTable.String_load_failed;
                image = ResourceTable.Media_icon_failed;
                Boolean networkConn = Util.isNetworkConnected(getContext());
                if (networkConn != null && !networkConn) {
                    str = ResourceTable.String_load_failed_no_network;
                    image = ResourceTable.Media_icon_no_wifi;
                }
                onClickListener = this;
                break;
            case Gloading.STATUS_EMPTY_DATA:
                stopImgRotation();
                str = ResourceTable.String_empty;
                image = ResourceTable.Media_icon_empty;
                break;
            default:
                break;
        }
        mImageView.setPixelMap(image);
        setClickedListener(onClickListener);
        mTextView.setText(str);
        setVisibility(show ? Component.VISIBLE : Component.HIDE);
    }

    private void startImgRotation() {
        animator.start();
    }

    private void stopImgRotation() {
        if (animator.isRunning()) {
            animator.cancel();
        }
    }

    @Override
    public void onClick(Component component) {
        if (mRetryTask != null) {
            mRetryTask.run();
        }
    }

}
